//
//  HXRevertViewController.m
//  KD
//
//  Created by Hubery on 16/2/17.
//  Copyright © 2016年 ifly. All rights reserved.
//

#import "HXRevertViewController.h"
#import "AWPlaceholderTextView.h"

@interface HXRevertViewController ()
@property (weak, nonatomic) IBOutlet UILabel *questionLabel;
@property (weak, nonatomic) IBOutlet UIView *vesselView;
@property (weak, nonatomic) IBOutlet UILabel *endTimeLabel;
@property (weak, nonatomic) IBOutlet UIButton *okBtn;
@property(nonatomic,weak) AWPlaceholderTextView *textView;
@property(nonatomic,strong) HXRevertQuestionAllListModel *data;
@end

@implementation HXRevertViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;

    [self loadData];
}
-(void)loadData{
    if ([GlobalUtil isNilValue:[UserDefaultHandler getUserDetault:kID]]) {
        [SVProgressHUD showErrorWithStatus:@"请登录"];
        return;
    }
    HXLog(@"%@     %@    %@",self.quesdata.ID,[UserDefaultHandler getUserDetault:kID],[NSString stringWithFormat:@"%d",UserTypeCorp]);
    [NetworkAPI revertListWithQuestID:self.quesdata.ID
                               UserId:[UserDefaultHandler getUserDetault:kID]
                             UserKind:[NSString stringWithFormat:@"%d",UserTypeCorp]
                              PageNum:@"0"
                             PageSize:@"-1"
                              success:^(HXRevertQuestionListModel *result) {
                                  if ([result.code isEqualToString:@"200"]) {
                                      self.data = result.anslist[0];
                                  }else if([result.code isEqualToString:@"400"]){//暂无数据
                                      self.data =nil;
                                  }else{//查询失败
                                      self.data = nil;
                                  }
                                  [self setUpView];
                              } failure:^(NSError *err) {
                                  self.data = nil;
                                  [self setUpView];
                                  [SVProgressHUD showErrorWithStatus:@"网络错误"];
                              }];
}

-(void)setUpView{
    if (self.data == nil) {//没回复
        [self setUpPlaceholderTextView];
        [self setUpEndTime];
        self.questionLabel.text = [NSString stringWithFormat:@"问题:%@",self.quesdata.Que_Content];
    }else{//已经回复
        UILabel *label = [[UILabel alloc]init];
        label.text = [NSString stringWithFormat:@"回复内容:%@",self.data.Content];
        label.frame = CGRectMake(0, 0, HXSCREEN_WIDTH-20, 30);
        [self.vesselView addSubview:label];
        self.questionLabel.text = [NSString stringWithFormat:@"问题:%@",self.quesdata.Que_Content];
        self.okBtn.enabled = NO;
        [self.okBtn setTitle:@"已经回复" forState:UIControlStateNormal];
        self.okBtn.backgroundColor = [UIColor lightGrayColor];
        self.endTimeLabel.text = @"该问题已经回复";
        [self.textView removeFromSuperview];
        }
}

-(void)setUpPlaceholderTextView{
    AWPlaceholderTextView *textView = [[AWPlaceholderTextView alloc]init];
    textView.placehoder = @"回复问题";
    textView.frame = CGRectMake(0, 0, HXSCREEN_WIDTH-20, 150);
    [self.vesselView addSubview:textView];
    self.textView = textView;
    
    

}

-(void)setUpEndTime{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    NSTimeZone* sourceTimeZone = [NSTimeZone systemTimeZone];
    dateFormatter.timeZone = sourceTimeZone;
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    int isEnd = [GlobalUtil compareDate:self.endDate withDate:[dateFormatter stringFromDate:[NSDate date]]];
    
    if (isEnd == -1){//可以回复
        self.okBtn.enabled = YES;
        self.okBtn.backgroundColor = MAINCOLOR;
        self.endTimeLabel.text = [NSString stringWithFormat:@"问题回复截止时间:%@",self.endDate];
    }else{
        self.endTimeLabel.text = @"该问题已经截止回复";
        [self.okBtn setTitle:@"不可回复" forState:UIControlStateNormal];
        [self.textView removeFromSuperview];
        self.okBtn.enabled = NO;
        self.okBtn.backgroundColor = [UIColor lightGrayColor];
    }
}

- (IBAction)revertBtnClick:(id)sender {
    if ([self.textView.text isEqualToString:@""]){
        [SVProgressHUD showErrorWithStatus:@"请输入回答内容"];
        return;
    }
    [NetworkAPI revertQuestionWithID:self.quesdata.ID
                   answer_MessUserID:self.tmUserID
                      answer_Content:self.textView.text
                             success:^(HXBaseModel *result) {
                                 if ([result.code isEqualToString:@"200"]) {
                                     [SVProgressHUD showSuccessWithStatus:@"回答成功"];
                                     [self.navigationController popToRootViewControllerAnimated:YES];
                                 }else{
                                 [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                 }
                             } failure:^(NSError *err) {
                                 [SVProgressHUD showErrorWithStatus:@"网络错误"];
                             }];
}

////date01 = date01 =>0
////date01 < date01 =>1
////date01 > date01 =>-1
//-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
//    int ci;
//    NSDateFormatter *df = [[NSDateFormatter alloc] init];
//    [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSDate *dt1 = [[NSDate alloc] init];
//    NSDate *dt2 = [[NSDate alloc] init];
//    dt1 = [df dateFromString:date01];
//    dt2 = [df dateFromString:date02];
//    NSComparisonResult result = [dt1 compare:dt2];
//    switch (result)
//    {
//        case NSOrderedAscending: ci=1; break;
//        case NSOrderedDescending: ci=-1; break;
//        case NSOrderedSame: ci=0; break;
//        default:  break;
//    }
//    return ci;
//}


@end

